import 'package:flutter/material.dart';
import 'package:flutter_love/bean/news_bean.dart';
import 'package:flutter_love/bean/detail_bean.dart';
import 'package:flutter_love/global/global.dart';
import 'package:flutter_love/net/dio_manager.dart';
import 'package:flutter_love/utils/page_route_util.dart';
import 'package:flutter_love/utils/progress_dialog_util.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:flustars/flustars.dart';
import 'package:flutter_love/generated/json/base/json_convert_content.dart';

class MessageView extends StatefulWidget {
  const MessageView({Key? key}) : super(key: key);

  @override
  _MessageViewState createState() => _MessageViewState();
}

class _MessageViewState extends State<MessageView> {
  bool _enablePullUp = true;
  final RefreshController _refreshController = RefreshController(initialRefresh: true);
  int page = 1;
  List newsList = [];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('消息'),
        centerTitle: true,
      ),
      body: SmartRefresher(
        //允许下拉
        enablePullDown: true,
        //允许上拉
        enablePullUp: _enablePullUp,
        //控制器
        controller: _refreshController,
        //刷新
        onRefresh: () {
          page = 1;
          _requestData();
        },
        //加载
        onLoading: () {
          page++;
          _requestData();
        },
        //列表
        child: ListView.separated(
            itemBuilder: listItem,
            separatorBuilder: (context, index) {
              return const Divider(
                color: Colors.black26,
              );
            },
            itemCount: newsList.length),
      ),
    );
  }

  Widget listItem(BuildContext context, int index) {
    if (newsList.isEmpty) return Container();
    NewsResultData data = newsList[index];
    return GestureDetector(
      behavior: HitTestBehavior.opaque,
      onTap: () {
        DetailBean bean = DetailBean();
        bean.title = data.title;
        bean.url = data.url;
        pushNameAndArguments(context, Global.routeWebDetail, bean);
      },
      child: Row(
        children: [
          Padding(
            padding: const EdgeInsets.all(10.0),
            child: Image(
              width: ScreenUtil.getScreenW(context) / 3,
              height: ScreenUtil.getScreenH(context) / 5,
              image: NetworkImage(data.thumbnailPicS),
              fit: BoxFit.cover,
            ),
          ),
          Expanded(
              child: Column(
            children: [
              Text(
                data.title,
                style: const TextStyle(color: Colors.black, fontSize: 14.0),
              ),
              const SizedBox(height: 20.0),
              Row(
                children: [
                  Text(
                    data.category,
                    style: const TextStyle(color: Colors.black26, fontSize: 12.0),
                  ),
                  const SizedBox(width: 10.0),
                  Text(
                    data.authorName,
                    style: const TextStyle(color: Colors.black26, fontSize: 12.0),
                  ),
                ],
              ),
              const SizedBox(height: 10.0),
              Container(
                alignment: Alignment.centerLeft,
                child: Text(
                  data.date,
                  style: const TextStyle(color: Colors.black26, fontSize: 12.0),
                ),
              ),
            ],
          )),
        ],
      ),
    );
  }

  void _requestData() {
    ProgressDialogUtil.showProgress(context);
    DioManager.getInstance()?.get('http://v.juhe.cn/toutiao/index', {
      'key': '39702145f3b76046a30b2d57b2c8b80e',
      'type': 'top',
      'page': page,
      'page_size': 10,
      'is_filter': 1,
    }, (data) {
      setState(() {
        ProgressDialogUtil.dismiss(context);
        NewsBean newsBean = JsonConvert.fromJsonAsT(data);
        List<NewsResultData> list = newsBean.result.data;
        if (page == 1) {
          newsList.clear();
          newsList.addAll(list);
          _refreshController.refreshCompleted();
        } else {
          if (list.isNotEmpty) {
            _enablePullUp = true;
          } else {
            _enablePullUp = false;
          }
          newsList.addAll(list);
          _refreshController.loadComplete();
        }
      });
    }, (error) {});
  }
}
